package com.xiaolong.common.utils;


import cn.hutool.core.util.StrUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Map;

public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;
    private static Environment environment = null;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
            environment = applicationContext.getEnvironment();
            //TODO 设置后，可以做一些操作
        }
    }

    public static ApplicationContext getApplicationContext() {
        return hasLoadContext() ? applicationContext : null;
    }
 
    public static Environment getEnvironment() {
        return environment;
    }

    private static boolean hasLoadContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("SpringContextUtil初始化失败,请在applicationContext.xml中定义SpringContextUtil");
        } else {
            return true;
        }
    }


    @SuppressWarnings("unchecked")
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
 
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

    @SuppressWarnings("unchecked")
    public static String getString(String key) {
        return getEnvironment().getProperty(key);
    }

    @SuppressWarnings("unchecked")
    public static Integer getInt(String key) {
        String property = getEnvironment().getProperty(key);
        if (StrUtil.isBlank(property)){
            return null;
        }
        return Integer.parseInt(property);
    }

    @SuppressWarnings("unchecked")
    public static Long getLong(String key) {
        String property = getEnvironment().getProperty(key);
        if (StrUtil.isBlank(property)){
            return null;
        }
        return Long.parseLong(property);
    }

    @SuppressWarnings("unchecked")
    public static boolean getBoolean(String key) {
        String property = getEnvironment().getProperty(key);
        if (StrUtil.isBlank(property)){
            return false;
        }
        return Boolean.parseBoolean(getEnvironment().getProperty(key));
    }

    @SuppressWarnings("unchecked")
    public static <T> T registerByProperties(String name, Class<T> clazz, Map<String, Object> params) {
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
        if (params != null && params.size() > 0) {
            params.forEach((s, o) -> {
                beanDefinitionBuilder.addPropertyValue(s, o);
            });
        }
        BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();

        BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry)applicationContext;
        beanFactory.registerBeanDefinition(name, beanDefinition);
        return applicationContext.getBean(name, clazz);
    }
}